//
//  TableViewController.m
//  UI_19_数据请求model嵌套
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "TableViewController.h"
#import "TableViewCell.h"
#import "FindCar.h"
#import "Result.h"
#import "Brandlist.h"
#import "Brand.h"
#import "UIImageView+DownloadImage.h"
#import "UpTableViewCell.h"
#import "SecondTableViewCell.h"
@interface TableViewController ()
@property (nonatomic, strong) FindCar *findCar;
@property (nonatomic, strong) UISearchBar*searchBar;
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self handleData];
     [self.tableView registerClass:[UpTableViewCell class] forCellReuseIdentifier:@"UICollectionViewCell"];
    [self.tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"SecondTableViewCell"];
}
#pragma mark - 数据处理
- (void)handleData{
    NSString *path  = [[NSBundle mainBundle] pathForResource:@"File" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    self.findCar = [FindCar modelWithDictionary:dic];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.findCar.result.brandlist.count + 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else if (section == 1) {
        return 1;
    } else {
        return self.findCar.result.brandlist[section - 2].list.count;
    }
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"热门汽车";
    } else if (section == 1) {
        return @"主打车";
    } else {
        return self.findCar.result.brandlist[section - 2].letter;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UICollectionViewCell"];
        return cell;
    } else if (indexPath.section == 1) {
        SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondTableViewCell"];
        return cell;
    } else {
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell " forIndexPath:indexPath];
    Brand *brand = self.findCar.result.brandlist[indexPath.section - 2].list[indexPath.row];
    NSString *imageUrl = brand.imgurl;
    [cell.myImageView asyncDownloadImageWithURL:imageUrl completionHandler:^(NSData *data, UIImage *image) {
        
    }];
    cell.myLabel.text = brand.name;
    return cell;
    }
}
#pragma mark - 每一行row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 120;
    } else if (indexPath.section == 1){
        return 80;
    } else {
        return 44;
    }
}
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray*titles=[[NSMutableArray alloc]init];
    //加上了放大镜，如果搜索框加到view上，让其不动的话，不用加
    [titles addObject:UITableViewIndexSearch];
    //加入其他内容
    for(int i=97;i<123;i++)
    {
        [titles addObject:[NSString stringWithFormat:@"%c",i]];
    }
    return titles;
}
#pragma mark - 设置索引条对应关系
//如果不加放大镜。这个方法就不需要。
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if(index==0)
    {
        //index是索引条的序号。从0开始，所以第0 个是放大镜。如果是放大镜坐标就移动到搜索框处
        [tableView scrollRectToVisible:_searchBar.frame animated:NO];
        return -1;
    }else{
        //因为返回的值是section的值。所以减1就是与section对应的值了
        return index-1;
    }
}
#pragma mark - 其他
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
